/**
 * Created by wushanxin on 14-4-8.
 */

var log = require("../lib/log");
var config = require('../config');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');


var db = require("../lib/db");
var operlog=require('../models/operlog.js');
exports.index = function(request, response) {

    var method = request.method || '';

    if(typeof(request.session.admin)=="undefined" || request.session.admin==null || request.session.admin.length==0)
    {
        response.send("<script>window.parent.location.href='login.html';</script>");
        return;
    }
    var operlogdata=RUtil.operlogdata(request);
    operlogdata.retdata.nickname =request.session.admin.nickname;
    operlogdata.retdata.roleinfo = request.session.roleinfo;
    operlogdata.retdata.userinfo = request.session.admin;
    if(method.toUpperCase() === "GET") {


        operlog.select(request.session.admin.id, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    operlogdata.retdata.info = "载入失败：系统内部错误(错误码："+error.errno+")";
                    operlogdata.retdata.success=false;
                    response.render("operlog", operlogdata);

                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    operlogdata.retdata.info = "没有任何操作日志!";
                    operlogdata.retdata.success=false;
                    operlogdata.operlog={};
                    response.render("operlog", operlogdata);
                } else {
                    //存在数据将数据发到页面
                    operlogdata.retdata.info = "操作日志提取成功!";
                    operlogdata.retdata.success=true;
                    operlogdata.operlog= results;

                    response.render("operlog", operlogdata);
                }
            });

    }

    else if(method.toUpperCase() === "POST") {
        if(request.body.oper=="deleteall")
        {
            operlog.deleteAll(request.session.admin.id, function(error,results){
                if((error)!== null)
                {
                    // 数据库异常
                    var info = { status:"success",info:'删除失败：系统内部错误(错误码：'+error.errno+')' };
                    response.send((info));
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    var info = { status:"error",info:'删除失败！' };
                    response.send( (info));
                } else {
                    //存在数据将数据发到页面
                    var info = { status:"success",info:'删除成功！' };
                    response.send( (info));
                }
            });

        }
        else if(request.body.oper == "del")
        {
            var delid = request.body.id;
            operlog.delete(delid, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    var info = { status:"success",info:'删除失败：系统内部错误(错误码：'+error.errno+')' };
                    response.send((info));
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    var info = { status:"error",info:'删除失败！' };
                    response.send( (info));
                } else {
                    //存在数据将数据发到页面
                    var info = { status:"success",info:'删除成功！' };
                    response.send( (info));
                }
            });
        }
    }

};


